 $(document).ready(function() {
             //		权限判断
		if($.cookie('ID') == null){
			window.location.href="http://www.duoweish.com/crm/login.html";
		}
//		2，3号权限
		if($.cookie('Right') == 1){
			window.location.href="http://www.duoweish.com/crm/index.html";
		}
 	$('.dw_name').text($.cookie('Name'));
		 $('.dw_part').text($.cookie('Department'));
			  var page_num = 0;		
             $(".select2").select2();
              $('#reservation').daterangepicker({
             	format:'YYYY-MM-DD',
             	startDate:'2017-10-10',
             	locale:{
             		format:'YYYY-MM-DD'
             	}
             });
             
             init(1);
             $('.search_btn').click(function(){
             	 
			  Ajax(1)
				
            })
           
          
            function Ajax(curr){
            	
            	 var Kind_data = 0;
			      var onoff = "True";//客户名or项目名
			      var sea_text =$.trim($('.search_text').val());//搜索框内容
				  var data_rang = $('.time_range').val();//时间范围
				  
				  //时间范围转化
				    var start = data_rang.split(' -')[0];
				  var end = data_rang.split('- ')[1];
				if($('.project_box').is(':checked')){
					Kind_data = 1;
					
				}else if($('.user_box').is(':checked')){
					Kind_data = 2;
				}else{
					Kind_data =0;
				}
				if(data_rang==""){onoff="False";}
				if(curr==1){
					onoff = "False";
				}
				
            	$.ajax({
				headers:{
				    	Accept:"application/json;charset=utf-8"
				    },
					type:"get",
					url:"http://www.duoweish.com/apis/BsManageSearch/",
					data:{
                            UserId:$.cookie('ID'),
							Date:onoff,
							DateBegin:start,
							DateEnd:end,
							Kind:Kind_data,
							Content:sea_text,
							page:curr||1,
							rows:8
					},
					success:function(data){
							if(data!='failure'){
							page_num = data[1];
							tableData(data);
							divePage(page_num,curr);
							}else{
								layer.msg('没有找到该用户');
								$('.search_text').val('');
							}
						}
				})
            	
            }
            function tableData(ret){
                    $('#table_body').empty();
                    
                    for(var i = 0;i < ret.length;i++){
                    	for (var j = 0;j<ret[i].length;j++) {
                    	
                        var tr = `<tr>
                                <td style="line-height:20px;">${ret[i][j][0]}</td>
                                <td style="line-height:20px;">${ret[i][j][1]}</td>
                                <td style="line-height:20px;">${ret[i][j][2]}</td>
                                <td style="line-height:20px;">${ret[i][j][3]}</td>
                                <td  index="${ret[i][j][4]}">
                                      <button class="edit_btn">查看</button>
                                </td>
                                <td index="${ret[i][j][4]}">
                                	<button class=" delect_btn">流亡</button>
                                	
                                </td>
                               
                            </tr>`;
                        $('#table_body').append(tr);
                    }
                }
                   //渲染完页面判断权限是否允许删除操作
                   
                   
                   
                   //编辑
                 $('.edit_btn').click(function(){
                 	var id_data = $(this).parent().attr("index");
                 console.log(id_data)
								      layer.open({
								        type: 2
								        ,title: false
								        ,closeBtn: 1
								        ,area:['100%','100%']
								        ,offset:['0','100%']
								        ,shade: 0
								        ,id: 'LAY_layuipro' 
								        ,btnAlign: 'c'
								        ,moveType: 1 
								        ,content:'http://www.duoweish.com/crm/user_infor.html?Id_data='+id_data
									    ,success: function(){
									    	$('#LAY_layuipro').parent().animate({left:'0'});
						               }
					    			    
							        })
						  
                      })
                   //删除 
                 $('.delect_btn').click(function(){
                 	if($.cookie('Right'!=='10086')){
                 		 layer.alert('您没有相关权限',{skin:'demo-class'})
                 	}else{
                 		var infor = $(this).parent().attr("index");
	                 	var self = $(this);
	                 	$.ajax({
	                 		type:"get",
	                 		url:"http://www.duoweish.com/apis/CstmDel/",
	                 		data:{
	                 			Id:infor
	                 		},
	                 		async:true,
	                 		success:function(data){
	                 			if(data=='success'){
	                 			
	                 			layer.msg('流亡成功',{time:1000},function(){
	                 				self.parent().parent().remove();
	                 			   });
	                 			}
	                 			
	                 			
	                 		}
	                 	});
                 	}
                 	
                 })
                    
            }
             layer.open({
						        type: 2
						        ,title: false
						        ,closeBtn: 1
						        ,area:['25%','100%']
						        ,offset:['0','100%']
						        ,shade: 0
						        ,id: 'LAY_layuipro2' 
						        ,shadeClose:true
						        ,btnAlign: 'c'
						        ,moveType: 1 
						        ,content:'http://www.duoweish.com/crm/new_buser.html?'
							   	,success:function(){
							   		
							   	}
					        })
			 
			// $(".layui-layer-iframe").hide();
			 $(".add_new").click(function(){
			 	//$(".layui-layer-iframe").show();
			 	$(".layui-layer-iframe").addClass('close_iframe');
			 	$(".layui-layer-iframe").animate({left:'75%'})
			 	
			 })
           function divePage(page_num,curr){
            layui.use(['laypage', 'layer'], function(){
					 var laypage = layui.laypage
			  		,layer = layui.layer;
                     laypage.render({
						    elem: 'demo0'
						    ,count: page_num
						    ,curr:curr
						    ,jump: function(obj, first){
						    	   if(!first){
						                Ajax(obj.curr);
						               }
				        
						      		}
						   		});
					 })		
           }
          
           function init(curr){ 
           	     Ajax(1)
	            divePage(page_num);
           }
         
     
           
})